# -*- coding: utf-8 -*-
# @Author   : zxc19
# @Time     : 2023/7/11 17:54
# @FileName : assert_util.py
# @Desc     : 请输入功能模块描述
import allure
from loguru import logger

from test_page.base.lsp_base_page import LSPBasePage
from utils.comparator_dict import comparator_dict
from utils.comparators import Comparator


class AssertUtil(object):
    @staticmethod
    @allure.step('执行断言')
    def exe_assert(expects, page_obj: LSPBasePage):
        for assert_el, assert_data in expects.items():
            if assert_el == 'table_page_turner':
                # 表格分页器
                table_page_turner_actual_data = page_obj.get_table_page_turner_assert_data()
                for expect_data in assert_data:
                    check_point = expect_data.get('check_point')
                    check_type = expect_data.get('check_type')
                    check_value = expect_data.get('check_value')
                    AssertUtil.exe_one_assert(check_type, table_page_turner_actual_data.get(check_point),
                                              str(check_value))
            elif assert_el == 'table_value':
                # 表格指定列的值
                for expect_data in assert_data:
                    check_col = expect_data.get('check_col')
                    # check_type = expect_data.get('check_type')
                    check_value = expect_data.get('check_value')
                    while True:
                        table_page_turner_actual_data = page_obj.get_table_page_turner_assert_data()
                        page_num = table_page_turner_actual_data.get('page_num')
                        page_current_num = table_page_turner_actual_data.get('page_current_num')
                        if table_page_turner_actual_data.get('end') == table_page_turner_actual_data.get('total'):
                            if page_obj.get_table_row_by_col_value(check_col, check_value):
                                break
                        try:
                            if page_obj.get_table_row_by_col_value(check_col, check_value):
                                break
                        except:
                            # 切换到下一页
                            if page_current_num < page_num:
                                page_obj.jump_table_page_turner(page_current_num + 1)
                            else:
                                raise AssertionError('未找到对应行')
            elif assert_el == 'message':
                # el-message
                el_message_actual_data = page_obj.get_el_message_assert_data()
                for expect_data in assert_data:
                    check_point = expect_data.get('check_point')
                    check_type = expect_data.get('check_type')
                    check_value = expect_data.get('check_value')
                    AssertUtil.exe_one_assert(check_type, el_message_actual_data.get(check_point), check_value)

    @staticmethod
    @allure.step('执行断言')
    def exe_one_assert(check_type: str, actual: str, expect: str):
        """
        执行断言
        :param check_type: 断言类型
        :param actual: 实际值
        :param expect: 期望值
        :return:h
        """
        # 运用反射进行断言方法的获取
        if hasattr(Comparator, check_type):
            func = getattr(Comparator, check_type)
            logger.debug(f'执行断言{check_type}-{comparator_dict.get(check_type)}')
            func(actual, expect)
        else:
            logger.warning(f'断言方法【{check_type}】未定义')
